<?php
require_once('includes/config.php');
require_once('includes/config.php');
include('includes/sc-includes.php');

$pagetitle = Dashboard;

if (empty($_GET['s']) && isset($_GET['s']))
{
	header('Location: '.$_SERVER['HTTP_REFERER']); die;
}

$cwhere = "";
if (isset($_GET['s']))
{
	$cwhere = "WHERE contact_first LIKE '%".addslashes($_GET['s'])."%' OR contact_last LIKE '%".addslashes($_GET['s'])."%' OR contact_email LIKE '%".addslashes($_GET['s'])."%' OR contact_company LIKE '%".addslashes($_GET['s'])."%'";
}

$query_contacts = "SELECT * FROM contacts $cwhere LIMIT 20";
$row_contacts = $sdb->select('contacts', $query_contacts);
$totalRows_contacts = count($row_contacts);

if ($totalRows_contacts < 1)
{
	header('Location: contact.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $pagetitle; ?></title>
<link href="includes/simplecustomer.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php include('includes/header.php'); ?>
<div class="container">
  <div class="leftcolumn">
<?php if ($search==1) { ?>
Search results for <em><?php echo $_GET['s']; ?></em>.<br />
<br />
<?php } ?>

<?php
	if ($totalRows_contacts > 0)
	{
?>
    	<h2>Recent Contacts</h2>
    	<?php
    		$i = 0;
    		do
    		{
		?>
				<a href="contact-details.php?id=<?php echo $row_contacts[$i]['Attributes']['contact_id']; ?>"><?php echo $row_contacts[$i]['Attributes']['contact_first']; ?> <?php echo $row_contacts[$i]['Attributes']['contact_last']; ?></a>
				<?php if ($totalRows_contacts != $i + 1) {?>,<?php } ?>
		<?php
				$i++;
			} while ($i < $totalRows_contacts); ?>
<?php
	}

	//
	// Get all notes sorted by date
	//
	$notes = array();
	$row_notes = $sdb->select('notes', "SELECT * from notes WHERE note_date != '' ORDER BY note_date DESC");

	if (count($row_notes) > 0)
	{
		//
		// 	Just use the top 5
		//
		$row_notes = array_slice($row_notes, 0, 5);
?>
		<hr/>
		<h2>Recent Notes  </h2>
		<?php
		foreach ($row_notes as $note)
		{ ?>
			<div <?php if ($note['Attributes']['note_date'] > time()-1) { ?>id="newnote"<?php } ?>>
        		<span class="datedisplay">
        		<a href="contact-details.php?id=<?php echo $note['Attributes']['contact_id']; ?>&note=<?php echo $note['Name']; ?>"><?php echo date('F d, Y', $note['Attributes']['note_date']); ?></a></span> for <a href="contact-details.php?id=<?php echo $note['Attributes']['contact_id']; ?>">
				<?php
					$contact_id = $note['Attributes']['contact_id'];
					$contact_attributes = $sdb->getAttributes('contacts', $contact_id);
					
					echo $contact_attributes['contact_first'];
				?>&nbsp;
				<?php
					echo $contact_attributes['contact_last'];
				?>
				</a>
				<br/>
				<?php echo $note['Attributes']['note_text']; ?>
			</div>
		<?php
		}?>
<?php } ?>

	<hr/>
	<h2>Contacts by Company</h2>
<?php

	//
	// Select the company
	//
	$query = "select contact_company from contacts";
	$results = $sdb->select('contacts', $query);

	$companies = array();
	foreach ($results as $result)
	{
		//
		// We don't really care about the value of the hash, we just
		// want to use the hash table to remove duplicates.
		//
		$companies[$result['Attributes']['contact_company']] = '0';
	}

	//
	// Get a count of each company
	//
	$countByCompany = array();
	foreach (array_keys($companies) as $company)
	{
		$query = "select count(*) from contacts where contact_company = '$company'";
		$results = $sdb->select('contacts', $query);

		$countByCompany[$company] = $results[0]['Attributes']['Count'];
	}

	//
	// Show company and counts
	//
	//http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World
	$chd = '';
	$chl = '';

	foreach ($countByCompany as $company => $count)
	{
		$chl .= $company . "|";
		$chd .= $count . ",";
	}

	//
	// Remove the trailing ,
	//
	$chd = substr_replace($chd, "", -1);

	print "<img src='http://chart.apis.google.com/chart?cht=p3&chco=A8DF7E&chd=t:$chd&chs=800x300&chl=$chl'/>";
?>

	<hr/>
	<h2>Contacts by Date</h2>
<?php
	//
	// Select the date when the contact was added
	//
	$query = "select timestamp from contacts";
	$results = $sdb->select('contacts', $query);

	$chd = '';
	foreach ($results as $result)
	{
		//
		// TODO this should really be 'd' for day, but since we added
		// all of these at once the graph would not be very interesting
		//
		$chd .= date('s', $result['Attributes']['timestamp']) . ',';
	}
	$chd = substr_replace($chd, "", -1);

	print "<img src='http://chart.apis.google.com/chart?cht=ls&chco=A8DF7E&chd=t:$chd&chs=800x300&chxt=y&chg=10,10'/>";
?>

  </div>
  <?php include('includes/right-column.php'); ?>
  <br clear="all" />
</div>

<?php include('includes/footer.php'); ?>

</body>
</html>